const Announcement = require('../model').Announcement

exports.getAllAnnInTeam = function(teamId, nSkip, nLimit) {
  return Announcement.find({ team: teamId, delete: false })
    .sort({ created_at: -1 })
    .skip(nSkip).limit(nLimit)
    .select('created_at icon title summary announcement_tag')
}

exports.countAnns = function(team) {
  return Announcement.count({ team, delete: false })
}

exports.removeAnnInTeam = function(annId, teamId) {
  return Announcement.findOneAndUpdate({
    _id: annId, team: teamId
  }, {
    delete: true
  })
}

exports.createAnn = function({ icon, title, summary, announcement_tag, content, author, team }) {
  return Announcement.create({ icon, title, summary, announcement_tag, content, author, team })
}

exports.getAnnInTeam = function(annId, teamId) {
  return Announcement.findOneAndUpdate({
    _id: annId, team: teamId
  }, {
    $inc: { read_times: 1 }
  }, { new: true }).populate({
    path: 'author',
    select: '-_id username'
  })
}
